package com.xpq.petserver.mapper;

import com.xpq.petserver.entity.DO.BailmentData;
import com.xpq.petserver.entity.DTO.PublicFosterageDTO;
import com.xpq.petserver.entity.DTO.SearchBailmentDataDTO;
import com.xpq.petserver.entity.VO.BailmentDetailVO;
import com.xpq.petserver.entity.VO.MyFosterageVO;
import com.xpq.petserver.entity.VO.SearchBailmentDataVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * BailmentData相关操作
 * @author XPQ
 * @date 2022-04-01
 */
@Repository
public interface BailmentDataMapper {
    /**
     * 添加数据
     * @param publicFosterageDTO
     */
    @Insert("insert into bailment_data "
            + "(id, title, pet_type,pet_sex, pet_month, linkman, phone, wechat, address, description, user_id, create_time, update_time, is_delete, code, adopt) "
            + "values (#{id},#{title},#{petType},#{petSex},#{petMonth},#{linkman},#{phone},#{wechat},#{address},#{description},#{userId},#{createTime},#{updateTime},#{isDelete},#{code},'0')")
    void insertBailmentData(PublicFosterageDTO publicFosterageDTO);

    /**
     * 编辑数据
     * @param publicFosterageDTO
     */
    @Update("update bailment_data set title = #{title}, pet_type = #{petType}, pet_sex = #{petSex}, pet_month = #{petMonth}, linkman = #{linkman}, phone = #{phone}, wechat = #{wechat}," +
            " address = #{address}, description = #{description}, "
            + "update_time = #{updateTime} where id = #{id}")
    void updateBailmentData(PublicFosterageDTO publicFosterageDTO);

    /**
     * 查询用户的寄养数据
     * @param userId 用户id
     * @return
     */
//    @Select("select id, title, pet_type,pet_sex, pet_month, create_time, is_delete from bailment_data where user_id = #{userId}")
    @Select({"<script>",
            "select id, title, pet_type,pet_sex, pet_month, create_time, is_delete, code, adopt from bailment_data",
            "WHERE user_id = #{userId}",
            "<when test='isDelete !=null and isDelete!=\"\" '>",
            "AND is_delete = #{isDelete}",
            "</when>",
            "</script>"})
    List<MyFosterageVO> selectMyFosterage(@Param("userId") String userId, @Param("isDelete") String isDelete);

    /**
     * 查询全部的寄养数据
     * @return
     */
//    @Select("select id, title, pet_type,pet_sex, pet_month, create_time, is_delete from bailment_data")
    @Select({"<script>",
            "select id, title, pet_type,pet_sex, pet_month, create_time, is_delete, code, adopt from bailment_data",
            "WHERE 1=1",
            "<when test='isDelete !=null and isDelete!=\"\" '>",
            "AND is_delete = #{isDelete}",
            "</when>",
            "</script>"})
    List<MyFosterageVO> selectMyFosterageAll(@Param("isDelete") String isDelete);


    @Select("select * from bailment_data where id = #{id} and code = #{code} ")
    BailmentDetailVO getBailmentDetailCode(@Param("id") Long id, @Param("code") String code);

    @Select("update bailment_data set adopt = '1' where id = #{id} and code = #{code} ")
    void updateBailmentDetailAdopt(@Param("id") Long id, @Param("code") String code);

    @Select("select * from bailment_data where id = #{id}")
    BailmentDetailVO selectBailmentDetailCode(@Param("id") Long id);

    @Select("select id, title, pet_type, pet_sex, pet_month, linkman, phone, wechat, address, description, status, user_id, create_time, update_time, is_delete, adopt from bailment_data where id = #{id}")
    BailmentDetailVO selectBailmentDetail(@Param("id") Long id);

    /**
     * 删除某条寄养数据
     * @param id 寄养数据id
     */
    @Update("update bailment_data set is_delete = '1' where id = #{id}")
    void deleteBailmentData(@Param("id") Long id);

    /**
     * 恢复某条寄养数据
     * @param id 寄养数据id
     */
    @Update("update bailment_data set is_delete = '0' where id = #{id}")
    void hfBailmentData(@Param("id") Long id);

    /**
     * 查询某一条寄养数据
     * @param id 寄养数据id
     * @return
     */
    @Select("select * from bailment_data where id = #{id}")
    BailmentData selectBailmentData(@Param("id") Long id);

    /**
     * 查询搜索的寄养数据
     * @param searchBailmentDataDTO
     * @return
     */
    List<SearchBailmentDataVO> selectSearchBailmentDataVO(SearchBailmentDataDTO searchBailmentDataDTO);


}
